package cn.jarlen.jwt.tools.antivirus;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jarlen
 * Create by 2024/7/3 11:23
 */
public class CVEHelper {

    public static long dateToStamp(String dateStr, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        long dateToStamp = 0;
        try {
            Date date = simpleDateFormat.parse(dateStr);
            dateToStamp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateToStamp;
    }

    public static String toFormatDate(String dateStr, String oriPattern, String targetPattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(oriPattern);
        try {
            Date date = simpleDateFormat.parse(dateStr);
            return new SimpleDateFormat(targetPattern).format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 比较两个版本
     *
     * @param pver 前版本号
     * @param cver 后版本号
     * @return 0 代表后版本小于前版本，1代表两版本相等，2代表后版本大于前版本 9 版本号格式错误
     */
    public static int compareVer(String pver, String cver) {
        if (TextUtils.isEmpty(pver) || TextUtils.isEmpty(cver)) {
            return 1;
        }
        try {
            String[] pvarr = pver.split("\\.");
            String[] cvarr = cver.split("\\.");
            int len = cvarr.length > pvarr.length ? pvarr.length : cvarr.length;

            for (int i = 0; i < len; i++) {
                if (Long.parseLong(cvarr[i]) < Long.parseLong(pvarr[i])) {
                    return 0;
                } else if (Long.parseLong(cvarr[i]) > Long.parseLong(pvarr[i])) {
                    return 2;
                }
            }
            if (cvarr.length > pvarr.length) {
                for (int j = pvarr.length; j < cvarr.length; j++) {
                    if (Long.parseLong(cvarr[j]) > 0) {
                        return 2;
                    }
                }
            } else if (cvarr.length < pvarr.length) {
                for (int j = cvarr.length; j < pvarr.length; j++) {
                    if (Long.parseLong(pvarr[j]) > 0) {
                        return 0;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    public static void copyAssetDatabase2PriPath(Context context) {

        /***/
        AssetManager manager = context.getAssets();
        String[] files;
        try {
            files = manager.list("");
            FileOutputStream outputStream = null;
            InputStream inputStream = null;
            byte[] buffer = new byte[1024];
            int length;
            for (String file : files) {
                if (file.endsWith(".db")) {
                    try {
                        inputStream = manager.open(file);
                        String distFilePath = context.getDatabasePath(file).getPath();
                        outputStream = new FileOutputStream(distFilePath);
                        while ((length = inputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, length);
                        }
                        outputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (inputStream != null) {
                            try {
                                inputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (outputStream != null) {
                            try {
                                outputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
